package com.yw.common.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yw.common.core.db.DbTypeEnum;
import com.yw.common.core.entity.StringParam;
import com.yw.common.core.fs.config.ConfigReaderFactory;
import com.yw.common.core.fs.config.IConfigReader;
import com.yw.common.core.fs.config.entity.DbConfig;
import com.yw.common.core.fs.config.exception.ConfigReaderException;

/**
 * 配置文件读取工具
 * @author aoshiguchen
 * @time 2015-08-27	
 */

public final class ConfigUtil {
	
	private static IConfigReader configReader = null;
	private static Map<String,String> config = null;
	private static DbConfig dbConfig = null;
	private static String dbType = null;
	
	
	static{
		init();
	}
	
	public static Map<String,String> readAllToStringMap(){
		
		if(null == config){
			config = getConfig();
		}
		
		return config;
	}
	
	public static List<StringParam> readAllToParamList(){
		List<StringParam> params = new ArrayList<StringParam>();
		
		if(null == config){
			config = getConfig();
		}
		
		for(String key : config.keySet()){
			params.add(new StringParam(key,config.get(key)));
		}
		
		return params;
	}
	
	public static Map<String,String> getConfig(){
		if(null == config){
			if(null == configReader){
				configReader = getConfigReader();
			}
			
			try {
				config = configReader.readAll();
			} catch (ConfigReaderException e) {
				e.printStackTrace();
			}
		}
		
		return config;
	}
	
	public static IConfigReader getConfigReader(){
		IConfigReader configReader = null;
		
		if(null == configReader){
			switch (ConstantUtil.getConfigFileType()) {
				case TXT:
					configReader = ConfigReaderFactory.createTxtConfigReader();
					break;
					
				case XML:
					configReader = ConfigReaderFactory.createXmlConfigReader();	
					break;
					
				case JSON:
					configReader = ConfigReaderFactory.createJsonConfigReader();
					break;
					
				case PROP:
					configReader = ConfigReaderFactory.createPropConfigReader();
					break;
					
				case INI:
					configReader = ConfigReaderFactory.createIniConfigReader();
					break;
				
			}
		}
		
		
		return configReader;
	}
	
	//初始化配置
	public static void init(){
		
		if(null == dbType){
			if(null == config){
				config = getConfig();
			}
			
			dbType = config.get(ConstantUtil.getDbTypeKeyName());
			
			if(dbType.equals("sqlserver")){
				ConstantUtil.setDbType(DbTypeEnum.SQLSERVER);
			}else if(dbType.equals("mysql")){
				ConstantUtil.setDbType(DbTypeEnum.MYSQL);
			}
		}
		
	}
	
	
	public static String getDbType(){
		if(null == dbConfig){
			dbConfig = getDbConfig();
		}
		
		return dbConfig.getDbType();
	}
	
	public static String getDbUrl(){
		if(null == dbConfig){
			dbConfig = getDbConfig();
		}
		
		return dbConfig.getDbUrl();
	}
	
	public static String getDbName(){
		if(null == dbConfig){
			dbConfig = getDbConfig();
		}
		
		return dbConfig.getDbName();
	}
	
	public static String getDbLoginName(){
		if(null == dbConfig){
			dbConfig = getDbConfig();
		}
		
		return dbConfig.getDbLoginName();
	}
	
	public static String getDbLoginPassword(){
		if(null == dbConfig){
			dbConfig = getDbConfig();
		}
		
		return dbConfig.getDbLoginPassword();
	}
	
	public static DbConfig getDbConfig(){
		if(null == dbConfig){
			if(null == config){
				config = getConfig();
			}
			
			dbConfig = new DbConfig();
			
			dbConfig.setDbType(config.get(ConstantUtil.getDbTypeKeyName()));
			dbConfig.setDbUrl(config.get(ConstantUtil.getDbHostKeyName()));
			dbConfig.setDbName(config.get(ConstantUtil.getDbNameKeyName()));
			dbConfig.setDbLoginName(config.get(ConstantUtil.getDbLoginNameKeyName()));
			dbConfig.setDbLoginPassword(config.get(ConstantUtil.getDbLoginPasswordKeyName()));
		}
		
		return dbConfig;
	}
	
}
